﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for VYC21BO
    /// </summary>
    public class VYC21BO:DBConnectionBO
    {
        public VYC21BO():base()
        {
            
        }
        public List<ModelVYC21> GetAllList()
        {
            var list = new List<ModelVYC21>();
            const string query = "SELECT [Id],[Ten],[TomTat],[IdSubLoaiChuyenMuc],[IdSubLoaiTour],[NgayCapNhat],[TrangThai],[AnhDaiDien] FROM [VYC-21]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC21();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? string.Empty : dataTable.Rows[i][0].ToString();
                model.Ten = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.TomTat = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.IdSubLoaiChuyenMuc = (dataTable.Rows[i][3] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][3]);
                model.IdSubLoaiTour = (dataTable.Rows[i][4] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][4]);
                model.NgayCapNhat = (dataTable.Rows[i][5] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][5];
                model.TrangThai = (dataTable.Rows[i][6] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][6]);

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Add new a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddData(ModelVYC21 model)
        {
            var query = "INSERT INTO [VYC-21]([Id],[Ten],[TomTat],[IdSubLoaiChuyenMuc],[IdSubLoaiTour],[NgayCapNhat]," +
                        "[TrangThai],[AnhDaiDien]) VALUES (@id, @ten, @tomtat,@idsubloaichuyenmuc,@idsubloaitour, GETDATE(), " +
                        "@trangthai, @anhdaidien)";
            var sqlCommand = new SqlCommand(query, connection);
            sqlCommand.Parameters.Add("@id", SqlDbType.VarChar).Value = model.Id;
            sqlCommand.Parameters.Add("@ten", SqlDbType.NVarChar).Value = model.Ten;
            sqlCommand.Parameters.Add("@tomtat", SqlDbType.NVarChar).Value = model.TomTat;
            sqlCommand.Parameters.Add("@idsubloaichuyenmuc", SqlDbType.Int).Value = model.IdSubLoaiChuyenMuc;
            sqlCommand.Parameters.Add("@idsubloaitour", SqlDbType.Int).Value = model.IdSubLoaiTour;
            sqlCommand.Parameters.Add("@trangthai", SqlDbType.Bit).Value = model.TrangThai;
            sqlCommand.Parameters.Add("@anhdaidien", SqlDbType.VarChar).Value = model.AnhDaiDien;
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Edit a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditData(ModelVYC21 model)
        {
            var query = "UPDATE [VYC-21] SET " +
                        "[Ten] = N'" + model.Ten + "', " +
                        "[TomTat] = N'" + model.TomTat + "', " +
                        "[IdSubLoaiChuyenMuc] = " + model.IdSubLoaiChuyenMuc + ", " +
                        "[IdSubLoaiTour] = " + model.IdSubLoaiTour + ", " +
                        "[NgayCapNhat] = GETDATE(), [TrangThai] = " + model.TrangThai +
                        ",[AnhDaiDien] = '" + model.AnhDaiDien + "'" +
                        " WHERE Id like '" + model.Id + "'";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteData(ModelVYC21 model)
        {
            var query = "DELETE FROM [VYC-21] " +
                        " WHERE Id like '" + model.Id + "'";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Get by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<ModelVYC21> GetById(string id)
        {
            var list = new List<ModelVYC21>();
            var query = "SELECT [Id],[Ten],[TomTat],[IdSubLoaiChuyenMuc],[IdSubLoaiTour],[NgayCapNhat],[TrangThai],[AnhDaiDien] FROM [VYC-21] WHERE Id like '" + id + "'";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC21();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? string.Empty : dataTable.Rows[i][0].ToString();
                model.Ten = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.TomTat = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.IdSubLoaiChuyenMuc = (dataTable.Rows[i][3] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][3]);
                model.IdSubLoaiTour = (dataTable.Rows[i][4] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][4]);
                model.NgayCapNhat = (dataTable.Rows[i][5] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][5];
                model.TrangThai = (dataTable.Rows[i][6] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][6]);
                model.AnhDaiDien = dataTable.Rows[i][7].ToString();
                list.Add(model);
            }
            return list;
        }

        public List<ModelVYC21> GetLimitAlbumForModule(int trangthai)
        {
            var list = new List<ModelVYC21>();
            try
            {
                var query = "SELECT TOP 10 * FROM [VYC-21] WHERE TrangThai = @trangthai Order by NgayCapNhat DESC";
                var sqlCommand = new SqlCommand(query, connection);
                sqlCommand.Parameters.Add("@trangthai", SqlDbType.Bit).Value = trangthai;
                var reader = sqlCommand.ExecuteReader();

                while (reader.Read())
                {
                    var model = new ModelVYC21();

                    model.Id = reader["Id"].ToString();
                    model.IdSubLoaiChuyenMuc = Convert.ToInt32(reader["IdSubLoaiChuyenMuc"]);
                    model.IdSubLoaiTour = Convert.ToInt32(reader["IdSubLoaiTour"]);
                    model.Ten = reader["Ten"].ToString();
                    model.TomTat = reader["TomTat"].ToString();                    
                    model.NgayCapNhat = (DateTime)reader["NgayCapNhat"];
                    model.TrangThai = Convert.ToInt32(reader["TrangThai"]);
                    model.AnhDaiDien = reader["AnhDaiDien"].ToString();

                    list.Add(model);
                }
                return list;
            }
            catch (Exception)
            {
                CloseConnection();
                throw;
            }
            finally
            {
                CloseConnection();
            }

        }

        public List<ModelVYC21> GetLimitAlbumForTourDetail(int trangthai, int idsubloaitour)
        {
            var list = new List<ModelVYC21>();
            try
            {
                var query = "SELECT TOP 10 * FROM [VYC-21] WHERE IdSubLoaiTour = @idsubloaitour and TrangThai = @trangthai Order by NgayCapNhat DESC";
                var sqlCommand = new SqlCommand(query, connection);
                sqlCommand.Parameters.Add("@idsubloaitour", SqlDbType.Int).Value = idsubloaitour;
                sqlCommand.Parameters.Add("@trangthai", SqlDbType.Bit).Value = trangthai;
                var reader = sqlCommand.ExecuteReader();

                while (reader.Read())
                {
                    var model = new ModelVYC21();

                    model.Id = reader["Id"].ToString();
                    model.IdSubLoaiChuyenMuc = Convert.ToInt32(reader["IdSubLoaiChuyenMuc"]);
                    model.IdSubLoaiTour = Convert.ToInt32(reader["IdSubLoaiTour"]);
                    model.Ten = reader["Ten"].ToString();
                    model.TomTat = reader["TomTat"].ToString();
                    model.NgayCapNhat = (DateTime)reader["NgayCapNhat"];
                    model.TrangThai = Convert.ToInt32(reader["TrangThai"]);
                    model.AnhDaiDien = reader["AnhDaiDien"].ToString();

                    list.Add(model);
                }
                return list;
            }
            catch (Exception)
            {
                CloseConnection();
                throw;
            }
            finally
            {
                CloseConnection();
            }

        }
    }
}